<template>
  <el-dialog
    :title="!dataForm.uuid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="cardId">
      <el-input v-model="dataForm.cardId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="personName">
      <el-input v-model="dataForm.personName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="posX">
      <el-input v-model="dataForm.posX" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="posY">
      <el-input v-model="dataForm.posY" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="posTime">
      <el-input v-model="dataForm.posTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isWork">
      <el-input v-model="dataForm.isWork" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uuid: 0,
          cardId: '',
          personName: '',
          posX: '',
          posY: '',
          posTime: '',
          isWork: ''
        },
        dataRule: {
          cardId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          personName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          posX: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          posY: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          posTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isWork: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uuid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uuid) {
            this.$http({
              url: this.$http.adornUrl(`/utypeone/utype1table/info/${this.dataForm.uuid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cardId = data.utype1Table.cardId
                this.dataForm.personName = data.utype1Table.personName
                this.dataForm.posX = data.utype1Table.posX
                this.dataForm.posY = data.utype1Table.posY
                this.dataForm.posTime = data.utype1Table.posTime
                this.dataForm.isWork = data.utype1Table.isWork
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/utypeone/utype1table/${!this.dataForm.uuid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uuid': this.dataForm.uuid || undefined,
                'cardId': this.dataForm.cardId,
                'personName': this.dataForm.personName,
                'posX': this.dataForm.posX,
                'posY': this.dataForm.posY,
                'posTime': this.dataForm.posTime,
                'isWork': this.dataForm.isWork
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
